﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SQLite;

public partial class SaleInfo_Winner : UDCPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            if (!String.IsNullOrEmpty(Request.QueryString["month"]))
            {
                ShowWinner();
            }
            else
            {
                TheDate.Text = GetThisDate();
            }
            LastDataBind();
            ThisDataBind();
        }
    }

    private void ShowWinner()
    {
        if (Request.QueryString["month"].ToString().Equals("this"))
        {
            WinnerThisList.Visible = true;
            WinnerLastList.Visible = false;
            ThisLink.Attributes.Add("class", "ui-btn-active ui-state-persist");
            LastLink.Attributes.Remove("class");
            TheDate.Text = GetThisDate();
        }
        else
        {
            WinnerLastList.Visible = true;
            WinnerThisList.Visible = false;
            LastLink.Attributes.Add("class", "ui-btn-active ui-state-persist");
            ThisLink.Attributes.Remove("class");
            TheDate.Text = GetLastDate();
        }
    }

    private void LastDataBind()
    {
        string sqlstr = @"select dvname,dname,count(1) as sales
                          from ud_saleinfo_t t1
                          left join ud_developer_t t2  
                            on t1.dvid = t2.dvid
                           and t2.ifvalid = 1    
                          left join ud_depart_t t3  
                            on t2.did = t3.did    
                         where date(saledate) between date('now','start of month','-1 month')
                                                  and date('now','start of month','-1 day')    
                           and isaudit = 1                       
                         group by dvname,dname 
                         order by count(1) desc";
        WinnerLastList.DataSource = SQLiteHelper.ExecuteDataset(sqlstr, null);
        WinnerLastList.DataBind();
    }

    private void ThisDataBind()
    {
        string sqlstr = @"select dvname,dname,count(1) as sales
                          from ud_saleinfo_t t1
                          left join ud_developer_t t2  
                            on t1.dvid = t2.dvid   
                           and t2.ifvalid = 1 
                          left join ud_depart_t t3  
                            on t2.did = t3.did    
                         where date(saledate) between date('now','start of month')
                                                  and date('now','start of month','+1 month','-1 day')    
                           and isaudit = 1                       
                         group by dvname,dname 
                         order by count(1) desc";
        WinnerThisList.DataSource = SQLiteHelper.ExecuteDataset(sqlstr, null);
        WinnerThisList.DataBind();
    }

    private string GetThisDate()
    {
        string sqlstr = @"select date(max(saledate)) as lastDate from ud_saleinfo_t
                         where date(saledate) between date('now','start of month')
                                                  and date('now','start of month','+1 month','-1 day')    
                           and isaudit = 1";
        string date = SQLiteHelper.ExecuteScalar(sqlstr, null).ToString();
        return date.Length > 0 ? "(截止" + date + ")" : "";
    }

    private string GetLastDate()
    {
        string sqlstr = @"select date(max(saledate)) as lastDate from ud_saleinfo_t
                         where date(saledate) between date('now','start of month','-1 month')
                                                  and date('now','start of month','-1 day')    
                           and isaudit = 1";
        string date = SQLiteHelper.ExecuteScalar(sqlstr, null).ToString();
        return date.Length > 0 ? "(截止" + date + ")" : "";
    }
}